package cn.rongdao.jrkabao.ui.cashadvance.fragment;

import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRToast;

import java.util.List;

import cn.rongdao.jrkabao.bean.cashadvance.CreditAndDepositCardBean;
import cn.rongdao.jrkabao.model.cashadvance.CreditModel;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.OnCreditCardClickListener;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddCreditCardForCashActivity;

/**
 * Created by jack on 2017/6/2
 */

public class FragmentCredit extends BaseCardFragment {

    @Override
    protected void initListener() {
        super.initListener();
        creditAndDepositCardAdapter.setOnCreditCardClickListener(new OnCreditCardClickListener() {
            @Override
            public void onDeleteClick(View v, int position) {
                if (creditAndDepositCardAdapter.isDefaultCreditCard(position) && creditAndDepositCardAdapter.getDataList().size() > 1) {
                    FRToast.showToastSafe("请重新设置主卡后再删除");
                } else {
                    showDeleteDialog(position, true);
                }
            }

            @Override
            public void onSettingClick(View v, int position) {
                if (creditAndDepositCardAdapter.isDefaultCreditCard(position)) {
                    return;
                }
                settingDefaultCard(position, true);
            }
        });

        tvAddCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddCreditCardForCashActivity.start(getContext());
            }
        });
    }

    @Override
    protected void requestData() {
        CreditModel.getInstance().execute(new FRBaseObserver<List<CreditAndDepositCardBean>>(this) {
            @Override
            protected void onBaseNext(List<CreditAndDepositCardBean> data) {
                if (null != data && data.size() > 0) {
                    creditAndDepositCardAdapter.setDataList(data);
                    if (tvAddCard != null) {
                        tvAddCard.setText("+ 添加新的信用卡");
                        tvAddCard.setVisibility(View.VISIBLE);
                    }
                } else {
                    runUiThreadSetEmptyVisible(true);
                }
                ptrRecyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                runUiThreadSetEmptyVisible(true);
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    @Override
    protected boolean isCredit() {
        return true;
    }
}